package com.app.piemoment.entity;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class Question {
	private String title;
	private String code;
	private String type;
	private String[] answers;

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String[] getAnswers() {
		return answers;
	}

	public void setAnswers(String[] answers) {
		this.answers = answers;
	}

	public void parser(JSONObject obj) throws JSONException {
		// {"type":"multiple_choice_one_answer","possible_answers":["完全不能接受。","确定会结婚之后才能接受。","可以接受。"],"title":"你能接受婚前同居吗？","code":"876131"}
		if (obj != null) {
			if(obj.has("code")) {
				setCode(obj.getString("code"));
			}
			if(obj.has("title")) {
				setTitle(obj.getString("title"));
			}
			if(obj.has("type")) {
				setType(obj.getString("type"));
			}
			if(obj.has("possible_answers")) {
				JSONArray array = obj.getJSONArray("possible_answers");
				if(array!=null&&array.length()>0) {
					answers = new String[array.length()];
					for(int i=0;i<array.length();i++) {
						answers[i] = (String) array.opt(i);
					}
					setAnswers(answers);
				}
			}
		}
	}
}
